package com.example.demo33.api;

import com.example.demo33.entity.Stats;
import com.example.demo33.entity.User;
import com.example.demo33.service.ItemService;
import com.example.demo33.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/stats")
public class StatsController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private UserService userService;

    // 获取用户统计数据
    @GetMapping("/user/{userId}")
    public ResponseEntity<Stats> getUserStats(@PathVariable Long userId) {
        // 验证用户权限
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();

        // 通过用户名查询对应的用户ID
        Long currentUserId = userService.getUserIdByUsername(username);

        // 获取统计数据
        Stats stats = itemService.getUserStats(currentUserId);
        return ResponseEntity.ok(stats);
    }
    
    // 从用户名获取用户ID
    private Long getUserIdFromUsername(String username) {
        // 实际项目中应从数据库查询
        return 1L; // 替换为真实逻辑
    }
}